// pages/work/index.js
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    work_list: [],
    
  },

  goWorkForm: function () {
    wx.navigateTo({
      url: '/pages/workForm/index',
    })
  },
  uploadwork:function () {
    wx.navigateTo({
      url: '/pages/uploadworkForm/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let is_manage = wx.getStorageSync('is_manage')
    if(is_manage===''){
      let openid = wx.getStorageSync('OPENID')
    wx.cloud.database().collection('users').where({_openid:openid}).get({
      success:res=>{
        is_manage = res.data[0].manage
        this.setData({
          is_manage: is_manage
        })
        wx.setStorageSync('is_manage', is_manage)
      }
    })
    }
    this.setData({
      is_manage: is_manage
    })
    

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.init({
      env: 'cloud1-5glocnmp62950062'
    })
    wx.cloud.callFunction({
      name: 'getInternlist',
      success: (res) => {
        console.log(res)
        wx.hideLoading({
          success: (res) => {},
        })
        let internlist = res.result.internlist.data

        this.setData({
          internlist: internlist
        })
      },
      fail: function (err) {
        wx.showToast({
          title: '获取数据失败，请稍后再试',
          icon: 'none',
        })
        console.error(err)
      },
      complete: res => {}
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  changetab: function (e) {
    let index = e.currentTarget.dataset.index
    let worktypelist = this.data.worktypelist
    if (index == 1) {
      worktypelist = worktypelist.filter(item => {
        return item.urgent == true
      })
    }
    if (index == 2) {
      worktypelist = worktypelist.filter(item => {
        return item.longtime == true
      })
    }
    if (index == 3) {
      let userInfo = wx.getStorageSync('userInfo')
      worktypelist = worktypelist.filter(item => {
        return item._openid == userInfo._openid
      })
    }

    this.setData({
      currentindex: index,
      currentworktypelist: worktypelist
    })
  },
  getsearchResult:function(e){
    let searchkey = e.detail
    this.setData({
      currentkey:searchkey
    })
    
    db.collection('internlist').where(
        db.command.or([
            {
          company_name: db.RegExp({
              regexp:searchkey,
              options: 'i'
            })
          },
          {
          work_title: db.RegExp({
              regexp:searchkey,
              options: 'i'
            })
          },
        {
          work_loaction: db.RegExp({
              regexp:searchkey,
              options: 'i'
            })
          },
        {
          salary: db.RegExp({
              regexp:searchkey,
              options: 'i'
            })
          },
        ])
    ).get({
      success:(res)=>{
        console.log(res)
        this.setData({
          internlist:res.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})